/*
 * Copyright (C) 2010 Yury Kudryashov.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package name.kudryashov.functions;

import name.kudryashov.base.Argument;

public class ComposableFunction3<A1, A2, A3, R> implements Function3<A1, A2, A3, R> {
    private final Function3<A1, A2, A3, R> f;

    public ComposableFunction3(Function3<A1, A2, A3, R> f) {
        this.f = Argument.requireNotNull(f, "f");
    }

    public R apply(A1 arg1, A2 arg2, A3 arg3) {
        return f.apply(arg1, arg2, arg3);
    }

    public String toString() {
        return f.toString();
    }

    public ComposableFunction2<A2, A3, R> bind1(final A1 arg) {
        return new ComposableFunction2<A2,A3,R>(new Function2<A2, A3, R>() {
            public R apply(A2 arg2, A3 arg3) {
                return f.apply(arg, arg2, arg3);
            }

            public String toString() {
                return String.format("%s(%s, _, _)", f, arg);
            }
        });
    }

    public ComposableFunction2<A1, A3, R> bind2(final A2 arg) {
        return new ComposableFunction2<A1, A3, R>(new Function2<A1, A3, R>() {
            public R apply(A1 arg1, A3 arg3) {
                return f.apply(arg1, arg, arg3);
            }

            public String toString() {
                return String.format("%s(_, %s, _)", f, arg);
            }
        });
    }

    public ComposableFunction2<A1, A2, R> bind3(final A3 arg) {
        return new ComposableFunction2<A1, A2, R>(new Function2<A1, A2, R>() {
            public R apply(A1 arg1, A2 arg2) {
                return f.apply(arg1, arg2, arg);
            }

            public String toString() {
                return String.format("%s(_, _, %s)", f, arg);
            }
        });
    }
}
